package com.feihao.zhou.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(value = {"/show.jsp", "/selectStudentServlet"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //获取是否有登录信息：
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        Object username = request.getSession().getAttribute("username");
        if (username == null || "".equals(username)) {
            //没有信息，重定向到首页界面：
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            return;
        } else {
            //如果有信息：放行：
            filterChain.doFilter(request, response);
        }

    }
}
